package com.bus.boot.dao.assist.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.bus.boot.dao.assist.BusGuestbookDao;
import com.bus.boot.entity.assist.BusGuestbook;
import com.bus.common.hibernate4.Updater;
import com.bus.common.jpa.Finder;
import com.bus.common.jpa.JpaBaseDao;
import com.bus.common.page.Pagination;

@Repository
public class BusGuestbookDaoImpl extends
		JpaBaseDao<BusGuestbook, Integer> implements BusGuestbookDao {
	public Pagination getPage(Integer siteId, Integer ctgId,Integer ctgIds[],
			Integer userId,Boolean recommend,Short checked, boolean asc,
			boolean cacheable, int pageNo,int pageSize) {
		Finder f = createFinder(siteId, ctgId, ctgIds,userId,recommend, checked, asc,
				cacheable);
		return find(f, pageNo, pageSize);
	}

	@SuppressWarnings("unchecked")
	public List<BusGuestbook> getList(Integer siteId, Integer ctgId,
			Integer userId,Boolean recommend, Short checked, boolean desc,
			boolean cacheable, int first, int max) {
		Finder f = createFinder(siteId, ctgId, null,userId,recommend, checked, desc,
				cacheable);
		f.setFirstResult(first);
		f.setMaxResults(max);
		return find(f);
	}

	private Finder createFinder(Integer siteId, Integer ctgId,Integer ctgIds[],Integer userId,
			Boolean recommend, Short checked, boolean desc, boolean cacheable) {
		Finder f = Finder.create("from BusGuestbook bean where 1=1");
		if (siteId != null) {
			f.append(" and bean.site.id=:siteId");
			f.setParam("siteId", siteId);
		}
		if (ctgId != null) {
			f.append(" and bean.ctg.id =:ctgId");
			f.setParam("ctgId", ctgId);
		}
		if(ctgIds!=null&&ctgIds.length>0){
			f.append(" and bean.ctg.id in(:ctgIds)");
			f.setParamList("ctgIds", ctgIds);
		}
		if (userId != null) {
			f.append(" and bean.member.id=:userId");
			f.setParam("userId", userId);
		}
		if (recommend != null) {
			f.append(" and bean.recommend=:recommend");
			f.setParam("recommend", recommend);
		}
		if (checked != null) {
			f.append(" and bean.checked=:checked");
			f.setParam("checked", checked);
		}
		if (desc) {
			f.append(" order by bean.id desc");
		} else {
			f.append(" order by bean.id asc");
		}
		f.setCacheable(cacheable);
		return f;
	}

	public BusGuestbook findById(Integer id) {
		BusGuestbook entity = get(id);
		return entity;
	}

	public BusGuestbook save(BusGuestbook bean) {
		getEntityManager().persist(bean);
		return bean;
	}

	public BusGuestbook deleteById(Integer id) {
		BusGuestbook entity = super.get(id);
		if (entity != null) {
			getEntityManager().remove(entity);
		}
		return entity;
	}

	@Override
	protected Class<BusGuestbook> getEntityClass() {
		return BusGuestbook.class;
	}

	@Override
	public BusGuestbook updateByUpdater(Updater<BusGuestbook> updater) {
		// TODO Auto-generated method stub
		return null;
	}
}